﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.OleDb
Imports System
Imports System.Globalization

Partial Class Interfaz_frmBodegaEntradaMateriales
    Inherits System.Web.UI.Page
    Dim CL As New Logica.ModuloBodega.ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            CL = Logica.ModuloBodega.ControladorLogica.getInstance

            If CL.visibilidadesFormsInternos("Bodega", Session("user").ToString) = False Then
                Response.Redirect("Index.aspx")
            End If
            Dim ArrayMaterial As New ArrayList
            Dim ArrayFamilia As New ArrayList
            Dim arrayOC As New ArrayList

            ArrayFamilia = CL.ObtenerFamilia()
            ArrayMaterial = CL.ObtenerMaterial()
            arrayOC = CL.ObtenerOrdenCompra()

            If Session("EntradaMateriales") Is Nothing Then
                Session("EntradaMateriales") = dtDatos
            Else
                dtDatos = DirectCast(Session("EntradaMateriales"), DataTable)

            End If

            If Not Page.IsPostBack Then
                CargarDropSeleccion()
                Me.FilaMaterial.Visible = False

                For i As Integer = 0 To ArrayFamilia.Count - 1
                    Dim itemFamilia As New ListItem

                    itemFamilia.Text = DirectCast(ArrayFamilia.Item(i), Familia).Nombre
                    itemFamilia.Value = DirectCast(ArrayFamilia.Item(i), Familia).Id
                    Me.DropFamilias.Items.Add(itemFamilia)
                Next

                For i As Integer = 0 To ArrayMaterial.Count - 1

                    Dim itemMaterial As New ListItem

                    itemMaterial.Value = DirectCast(ArrayMaterial.Item(i), Material).ID
                    itemMaterial.Text = DirectCast(ArrayMaterial.Item(i), Material).Nombre
                    Me.DropCodigoMateriales.Items.Add(itemMaterial)
                Next

                For i As Integer = 0 To arrayOC.Count - 1

                    Dim itemOC As New ListItem

                    itemOC.Text = DirectCast(arrayOC.Item(i), OrdenCompra).NumeroDeOrden
                    itemOC.Value = DirectCast(arrayOC.Item(i), OrdenCompra).NumeroDeOrden
                    Me.DropOC.Items.Add(itemOC)
                Next
                CargarDropFilaColumna()

                dtDatos.Columns.Add(New DataColumn("ITEM"))
                dtDatos.Columns.Add(New DataColumn("UNIDAD"))
                dtDatos.Columns.Add(New DataColumn("DETALLE"))
                dtDatos.Columns.Add(New DataColumn("CANTIDAD"))
                dtDatos.Columns.Add(New DataColumn("PRECIOUNITARIO"))
                dtDatos.Columns.Add(New DataColumn("CODMATERIAL"))
                dtDatos.Columns.Add(New DataColumn("FILA"))
                dtDatos.Columns.Add(New DataColumn("COLUMNA"))

                Me.GridBodega.DataBind()
                Me.GridBodega.DataSource = Me.dtDatos
            End If
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub


#Region "Funcionalidades"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub CargarDropFilaColumna()
        Me.DropFila.Items.Add("A")
        Me.DropFila.Items.Add("B")
        Me.DropFila.Items.Add("C")
        Me.DropFila.Items.Add("D")
        Me.DropFila.Items.Add("E")
        Me.DropFila.Items.Add("F")
        Me.DropFila.Items.Add("G")
        Me.DropFila.Items.Add("H")
        Me.DropFila.Items.Add("I")
        Me.DropFila.Items.Add("J")
        Me.DropFila.Items.Add("K")
        Me.DropFila.Items.Add("L")
        Me.DropFila.Items.Add("M")
        Me.DropFila.Items.Add("N")
        Me.DropFila.Items.Add("Ñ")
        Me.DropFila.Items.Add("O")
        Me.DropFila.Items.Add("P")
        Me.DropFila.Items.Add("Q")
        Me.DropFila.Items.Add("R")
        Me.DropFila.Items.Add("S")
        Me.DropFila.Items.Add("T")
        Me.DropFila.Items.Add("U")
        Me.DropFila.Items.Add("V")
        Me.DropFila.Items.Add("W")
        Me.DropFila.Items.Add("X")
        Me.DropFila.Items.Add("Y")
        Me.DropFila.Items.Add("Z")

        For i As Integer = 1 To 20
            Me.DropColumna.Items.Add(i)
        Next
    End Sub

    Private Sub CargarDropSeleccion()
        Try
            Dim list As New ListItem

            list.Value = "Manual"
            list.Text = "Manual"

            Me.DropTipoIngreso.Items.Add(list)

            list.Value = "Automatico"
            list.Text = "Automatico"

            Me.DropTipoIngreso.Items.Add(list)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Limpiar()
        Me.TxtCantidad.Text = ""
        Me.TxtDetalle.Text = ""
    End Sub

    Private Sub InsertarBD()
        Try
            Dim filaGrid As GridViewRow
            Dim Nombre As String
            Dim Descripcion As String = Me.TxtDetalle.Text
            Dim Cantidad As String = Me.TxtCantidad.Text
            Dim FechaIngreso As String = Me.Calendario1.obtieneValorFecha
            Dim UbicacionColumna As String = Me.DropFila.Text
            Dim UbicacionFila As String = Me.DropColumna.Text
            Dim indicador As String
            Dim query As String

            Dim MaterialDatos As New MaterialDatos()

            If Me.DropTipoIngreso.SelectedValue = "Manual" = 0 Then
                MaterialDatos.Cantidad = Cantidad
                MaterialDatos.FechaIngreso = FechaIngreso
                MaterialDatos.UbicacionColumna = UbicacionColumna
                MaterialDatos.UbicacionFila = UbicacionFila
                MaterialDatos.FkBodega = "1"
                MaterialDatos.FkOrdenCompra = ""
                MaterialDatos.Descripcion = Me.TxtDetalle.Text

                If CheckCodigo.Checked = True Then
                    MaterialDatos.FkMaterial = Me.DropCodigoMateriales.SelectedValue
                    CL.InsertarMaterialDatos(MaterialDatos)
                Else
                    If Me.CheckFamilia.Checked = True Then
                        MaterialDatos.FkMaterial = Me.DropMaterial.SelectedValue
                        CL.InsertarMaterialDatos(MaterialDatos)
                    End If
                End If
            Else
                With Me.GridBodega
                    ' Recorrer las filas del dataGridView  
                    For fila As Integer = 0 To .Rows.Count - 1
                        filaGrid = Me.GridBodega.Rows(fila)
                        Dim cb As CheckBox = filaGrid.FindControl("itemSelector")

                        'Recorrer la cantidad de columnas que contiene el dataGridView 
                        If cb.Checked = True Then

                            Dim item As String = filaGrid.Cells(1).Text
                            Dim unidad As String = filaGrid.Cells(2).Text
                            Dim detalle As String = filaGrid.Cells(3).Text
                            Dim cant As String = filaGrid.Cells(4).Text
                            Dim precioUnitario As String = filaGrid.Cells(5).Text
                            Dim codMat As String = filaGrid.Cells(6).Text
                            Dim fil As String = filaGrid.Cells(1).Text
                            Dim columna As String = filaGrid.Cells(8).Text
                            Dim detal As String = filaGrid.Cells(9).Text

                            MaterialDatos.Cantidad = cant
                            MaterialDatos.FechaIngreso = FechaIngreso
                            MaterialDatos.UbicacionColumna = fil
                            MaterialDatos.UbicacionFila = columna
                            MaterialDatos.FkBodega = "1"
                            MaterialDatos.FkOrdenCompra = DropOC.Text
                            MaterialDatos.Descripcion = detal

                            MaterialDatos.FkMaterial = DirectCast(CL.ObtenerMaterial(codMat).Item(0), Material).ID
                            CL.InsertarMaterialDatos(MaterialDatos)
                        End If
                    Next fila
                End With

            End If

            Console.Write("El ingreso del material se ha realizado correctamente")
            indicador = ""
            Limpiar()
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

    Private Sub CargarOrdenCompra()
        Try
            Dim numeroOC As String = Me.DropOC.Text
            Dim arrayOC As ArrayList = CL.ObtenerOrdenCompra(numeroOC)
            Dim arrayItemOC As ArrayList = CL.obtenerItemOrdenCompra(numeroOC)
            Me.TxTFechaPedido.Text = DirectCast(arrayOC.Item(0), OrdenCompra).FechaDePedido

            Dim i As Integer
            vaciaGrid()

            For i = 0 To arrayItemOC.Count - 1
                Dim drDatos As DataRow = dtDatos.NewRow
                Dim item As ItemDocumentos = DirectCast(arrayItemOC.Item(i), ItemDocumentos)
                Dim codMaterial As String = item.Fkmaterial

                drDatos.Item("ITEM") = item.ITEM
                drDatos.Item("UNIDAD") = item.Unidad
                drDatos.Item("DETALLE") = item.Detalle
                drDatos.Item("CANTIDAD") = item.Cantidad
                drDatos.Item("PRECIOUNITARIO") = item.PrecioUnitario
                drDatos.Item("CODMATERIAL") = codMaterial
                drDatos.Item("FILA") = "Inserte Fila"
                drDatos.Item("COLUMNA") = "Inserte Columna"

                dtDatos.Rows.Add(drDatos)

                Me.GridBodega.DataSource = dtDatos
                Me.GridBodega.DataBind()
            Next
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

    Private Sub BuscarItemFamilia()
        Try
            Dim idFamilia As String = Me.DropFamilias.SelectedValue
            Dim arrayMaterial As ArrayList = CL.ObtenerFamiliaMaterial(idFamilia)

            Me.DropMaterial.Items.Clear()
            If arrayMaterial.Count <> 0 Then
                For i As Integer = 0 To arrayMaterial.Count - 1
                    Dim item As Material = DirectCast(arrayMaterial.Item(0), Material)
                    Me.DropMaterial.Items.Add(item.Nombre)
                Next
            Else
                Me.Mensaje("No hay materiales asociados a la familia seleccionada")
            End If
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
        
    End Sub

    Private Sub vaciaGrid()
        Try
            dtDatos = DirectCast(Session("EntradaMateriales"), DataTable)
            If Not dtDatos Is Nothing Then
                dtDatos.Clear()
                Session("EntradaMateriales") = dtDatos
            End If
            Me.GridBodega.DataSource = Nothing
            Me.GridBodega.DataBind()
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

#End Region

#Region "Botones"

    Protected Sub BtnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBuscar.Click
        Me.FilaMaterial.Visible = True
        BuscarItemFamilia()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        InsertarBD()
    End Sub

    'Protected Sub BttnEscojer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BttnEscojer.Click
    '    Escojer()
    'End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Limpiar()
    End Sub

    Protected Sub BtnOC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOC.Click
        CargarOrdenCompra()
    End Sub
#End Region

#Region "Eventos"
    Protected Sub CheckFamilia_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckFamilia.CheckedChanged
        If CheckFamilia.Checked Then
            Me.CheckCodigo.Checked = False
        End If
    End Sub

    Protected Sub CheckCodigo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckCodigo.CheckedChanged
        If CheckCodigo.Checked Then
            Me.CheckFamilia.Checked = False
            Me.FilaMaterial.Visible = False
        End If
    End Sub
#End Region

#Region "Eventos Grid"
    'Grid Material
    Protected Sub GridBodega_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridBodega.PageIndex = e.NewPageIndex
        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Protected Sub GridBodega_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'Set the edit index.
        GridBodega.EditIndex = e.NewEditIndex
        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Protected Sub GridBodega_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        'Reset the edit index.
        GridBodega.EditIndex = -1
        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Protected Sub GridBodega_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        'Retrieve the table from the session object.
        Dim dt As DataTable = CType(Session("EntradaMateriales"), DataTable)

        'Update the values.
        Dim row As GridViewRow = GridBodega.Rows(e.RowIndex)

        Dim ITEM As String = (CType((row.Cells(1).Controls(0)), TextBox)).Text.ToString
        Dim UNIDAD As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text
        Dim DETALLE As String = (CType((row.Cells(3).Controls(0)), TextBox)).Text
        Dim CANTIDAD As String = (CType((row.Cells(4).Controls(0)), TextBox)).Text
        Dim PRECIOUNITARIO As String = (CType((row.Cells(5).Controls(0)), TextBox)).Text
        Dim CODMATERIAL As String = (CType((row.Cells(6).Controls(0)), TextBox)).Text
        Dim FILA As String = (CType((row.Cells(8).Controls(0)), TextBox)).Text
        Dim COLUMNA As String = (CType((row.Cells(9).Controls(0)), TextBox)).Text


        dt.Rows(row.DataItemIndex)("ITEM") = ITEM
        dt.Rows(row.DataItemIndex)("UNIDAD") = UNIDAD
        dt.Rows(row.DataItemIndex)("DETALLE") = DETALLE
        dt.Rows(row.DataItemIndex)("CANTIDAD") = CANTIDAD
        dt.Rows(row.DataItemIndex)("PRECIOUNITARIO") = PRECIOUNITARIO
        dt.Rows(row.DataItemIndex)("CODMATERIAL") = CODMATERIAL
        dt.Rows(row.DataItemIndex)("FILA") = FILA
        dt.Rows(row.DataItemIndex)("COLUMNA") = COLUMNA

        'Reset the edit index.
        GridBodega.EditIndex = -1

        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Sub GridBodega_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim dt As DataTable = CType(Session("EntradaMateriales"), DataTable)
        dt.Rows(e.RowIndex).Delete()
        GridBodega.DataSource = dt
        GridBodega.DataBind()
    End Sub
#End Region

    
  
End Class
